package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.RoomDTO;
import com.wjj.sixinone.common.pojo.entity.Room;
import com.wjj.sixinone.common.pojo.excel.RoomExcel;
import com.wjj.sixinone.common.pojo.vo.RoomForm;
import com.wjj.sixinone.common.pojo.vo.RoomVO;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.mapper.RoomMapper;
import com.wjj.sixinone.service.RoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小王不秃头
 * @since 2024-07-22
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private ExcelExportHandle excelHandler;

    @Override
    public Result selectRoomByLimit(RoomVO roomVO) {
        Integer page = roomVO.getPage();
        Integer limit = roomVO.getLimit();
        Integer index=(page - 1)*limit;
        RoomDTO roomDTO = new RoomDTO();
        BeanUtils.copyProperties(roomVO, roomDTO);
        roomDTO.setIndex(index);
       List<Room> roomList= roomMapper.selectRoomByLimit(roomDTO);
        Integer count=roomMapper.selectCount(roomDTO);

        return Result.ok().put("data", roomList).put("count", count);
    }

    @Override
    public Result updateRoomById(RoomForm roomForm) {
        int id = roomForm.getId();
        String roomNo = roomForm.getRoomNo();
        checkRoomNo(roomNo, id);

        Room room = new Room();
        BeanUtils.copyProperties(roomForm,room);
        int i = roomMapper.updateById(room);
        if(i<1){
            throw new GlobalException(ExceptionEnum.UPDATE_ERROR);
        }
        return Result.ok();
    }

    @Override
    public Result addRoom(RoomForm roomForm) {
        int id = roomForm.getId();
        String roomNo = roomForm.getRoomNo();
        checkRoomNo(roomNo, id);


        Room room = new Room();
        BeanUtils.copyProperties(roomForm,room);
        int i = roomMapper.insert(room);
        if (i<1){
            throw new GlobalException(ExceptionEnum.INSERT_ERROR);
        }

        return Result.ok();
    }

    @Override
    public void uploadExcel(HttpServletResponse httpServletResponse) {
        List<Room> roomList = roomMapper.selectList(null);
        List<RoomExcel> roomExcels=new ArrayList<>();
        for(Room room:roomList){
            RoomExcel roomExcel=new RoomExcel();
            BeanUtils.copyProperties(room,roomExcel);
            roomExcels.add(roomExcel);
        }

        excelHandler.export(httpServletResponse,"病房信息",roomExcels,RoomExcel.class);

    }

    @Override
    public Result deleteRoomById(Map<String, String> map) {
        String id = map.get("id");
        if(id!=null){
            int i = roomMapper.deleteById(id);
            if(i<1){
                throw new GlobalException(ExceptionEnum.DELETE_ERROR);
            }
        }
        return Result.ok();
    }




    /**
     * 校验房间号是否已被占用
     *
     * @param roomNo 房间号，用于查询房间信息
     * @param id 当前操作的房间ID，用于对比是否为同一房间
     * @throws GlobalException 如果房间号已被其他房间占用，则抛出异常
     *
     * 通过这个方法了，用户要么是操作自己的房间要么是没被用的房间号
     */

    private void checkRoomNo(String roomNo, int id) {
        // 根据房间号查询所有房间信息，只选取房间ID
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>()
                .select(Room::getId)
                .eq(Room::getRoomNo, roomNo));

        // 如果查询结果不为空
        if (roomList != null) {
            // 遍历查询到的房间列表
            for (Room room : roomList) {
                // 如果当前遍历到的房间ID不等于当前操作的房间ID，则表示房间号已被占用
                if (room.getId() != id) {
                    throw new GlobalException("该房间号已被占用", 400);
                }
            }
        }
    }
}
